﻿using mb;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly : ResolutionGroupName("com.jj")]

[assembly : ExportEffect(typeof(iOSLongPressedEffect), "PressedEffect")]
public class iOSLongPressedEffect : PlatformEffect
{
    private bool _attached;
    private readonly UILongPressGestureRecognizer _longPressRecognizer;

    public iOSLongPressedEffect()
    {
        _longPressRecognizer = new UILongPressGestureRecognizer(HandleLongClick);
    }

    protected override void OnAttached()
    {
        if (!_attached)
        {
            Container.AddGestureRecognizer(_longPressRecognizer);
            _attached = true;
        }
    }

    private void HandleLongClick()
    {
        if (_longPressRecognizer.State != UIGestureRecognizerState.Began)
            return;
        var command = PressedEffect.GetLongPressCommand(Element);
        command?.Execute(null);
    }

    protected override void OnDetached()
    {
        if (_attached)
        {
            Container.RemoveGestureRecognizer(_longPressRecognizer);
            _attached = false;
        }
    }

}